import { Body, Controller, Get, Post,UploadedFile, UseInterceptors, Redirect, Query, Param, Req, HttpException, HttpStatus, BadRequestException  } from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto, PickUserDto } from './dto/create-user.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { Express } from 'express'
import { Observable, of } from 'rxjs';


// 简写形式
@Controller('user')

// 完整形式
// @Controller({
//     path: 'user',
//     host: 'daefebur-80ut1f80-ipsd19gt11o8.vcc4p.mcprev.cn',
// })
export class UserController {
    constructor(private readonly userService: UserService) {}
    @Post('createUser')
    create(@Body() createUserDto: CreateUserDto) {
        return this.userService.create(CreateUserDto);
        

        return 'create user';
    }

    @Post('updateUser')
    update(@Body() updateUserDto: PickUserDto) {
       
        return 'updateUser';
    }

    @Get()
    findAll() {
        return 'find all';
    }

    @Post('upload')
    @UseInterceptors(FileInterceptor('file'))
    uploadFile(@UploadedFile() file: Express.Multer.File) {
        console.log('file');
        
        return 'upload file';
    }

    // 动态路由参数
    @Get('docs/:id')
    getDocs(@Param() params: any) {
        return params;
    }

    // 查询参数
    @Get('info')
    getInfo(@Query('name') name: any, @Req() req: any) {
        console.log(req);
        
        return 111;
    }

    // @Get('list')
    // async getList() {
    //     let res = await new Promise((resolve) => {
    //         setTimeout(() => {
    //             resolve(1);
    //         }, 2000);
    //     });
    //    return res;
    // }

    // RxJS 可观察流
    @Get('list')
    getList(): Observable<any[]> {

        let res = of([1,2,3]);
        return res;
    }

    @Get('pist')
    getsList(): Observable<any[]> {
        let res = of([1,2,3]);

        // throw new HttpException({
        //     status: HttpStatus.FORBIDDEN,
        //     error: 'This is a custom messagess',
        //   }, HttpStatus.FORBIDDEN, {
        //     cause: '222'
        //   });
        // throw new BadRequestException('Something bad happened111');
        throw new BadRequestException('Something bad happened', {
            cause: new Error(),
            description: 'Some error descriptionsss',
          });
        return res;
    }

}
